Android Learning audio, Video

Source: Internet
Author: User
<span id="Label3"></p>Play Audio<p><p>Playback audio in Android can be implemented using the MediaPlayer class, which is some of its methods:</p></p><p><p>      </p></p><p><p>Method Name Function Description<br>Setdatasource () Sets the location of the audio file to Play.<br>Prepare () calls this method to complete the preparation before starting Playback.<br>Start () to begin or resume audio playback.<br>Pause () pauses audio playback.<br>Reset () Resets the MediaPlayer object to the state you just Created.<br>Seekto () plays the audio starting at the specified Position.<br>Stop () stops playing Audio. The MediaPlayer object after calling this method can no longer play audio.<br>Release () releases the resources associated with the MediaPlayer Object.<br>IsPlaying () Determines whether the current MediaPlayer is playing Audio.<br>Getduration () Gets the length of the loaded audio File.</p></p><p><p>Now give a piece of code:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mainactivity<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span>Activity<span style="color: #0000ff;"><span style="color: #0000ff;">Implements</span></span><span style="color: #000000;"><span style="color: #000000;">View.onclicklistener {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Button play; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Button pause; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Button stop; </span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">MediaPlayer class can used to control playback of audio/video files and Streams. </span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>MediaPlayer MediaPlayer =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">MediaPlayer (); @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Get Control</span></span>Play =<span style="color: #000000;"><span style="color: #000000;">(Button) Findviewbyid (r.id.play); Pause</span></span>=<span style="color: #000000;"><span style="color: #000000;">(Button) Findviewbyid (r.id.pause); Stop</span></span>=<span style="color: #000000;"><span style="color: #000000;">(Button) Findviewbyid (r.id.stop); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Initialize MediaPlayer</span></span><span style="color: #000000;"><span style="color: #000000;">Initmediaplayer (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Setting the response</span></span>Play.setonclicklistener (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); Pause.setonclicklistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); Stop.setonclicklistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnClick (View V) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Switch</span></span><span style="color: #000000;"><span style="color: #000000;">(v.getid ()) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">r.id.play:</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">If the current video is not playing, play the Video. </span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">mediaplayer.isplaying ()) {mediaplayer.start (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">r.id.pause:</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">If the current video is playing, pause</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(mediaplayer.isplaying ()) {mediaplayer.pause (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">r.id.stop:</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(mediaplayer.isplaying ()) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Stop playing</span></span><span style="color: #000000;"><span style="color: #000000;">Mediaplayer.reset (); Initmediaplayer (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">default</span></span><span style="color: #000000;"><span style="color: #000000;">: </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; }} @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnDestroy () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(mediaPlayer! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {mediaplayer.release (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnDestroy (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">initmediaplayer () {file file</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>File (environment.getexternalstoragedirectory (), "test.mp3"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{mediaplayer.setdatasource (file.getpath ());</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">specify the path to the audio file</span></span>Mediaplayer.prepare ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get MediaPlayer into the prep State</span> .</span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">toast.maketext (mainactivity.this, "succeed", toast.length_short). show ();</span></span>}<span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(ioexception E) {e.printstacktrace (); } }}</span></span></pre><p><p></p></p><p><p></p></p><p><p>Mediaplayer.setdatasource set the exact location of the audio, and then call the Mediaplayer.prepare () method, indicating that MediaPlayer is Ready. You can then call Mediaplayer's start (), pause (), reset () to start the audio separately, pause the audio, reset the MediaPlayer to the creation State.</p></p><p><p>note, of course, that we are reading the data on the SD card to set</p></p><pre><pre><span style="color: #0000ff;"><</span><span style="color: #800000;"></span><span style="color: #ff0000;">android:name</span><span style="color: #0000ff;">= "android.permission.WRITE_EXTERNAL_STORAGE"</span><span style="color: #0000ff;">/></span></pre></pre><p><p></p></p><p><p></p></p>Play Video<p><p>Playback video can be implemented using the Videoview class, which has both display and Control.</p></p><p><p>  </p></p><p><p>Method Name Function Description<br>Setvideopath () Sets the location of the video file to Play.<br>Start () starts or resumes playing the Video.<br>Pause () pauses playback of the Video.<br>Resume () starts playback of the video from the Beginning.<br>Seekto () plays the video starting at the specified Position.<br>IsPlaying () Determines whether the video is currently Playing.<br>Getduration () Gets the length of the loaded video File.</p></p><p><p></p></p><p><p>Videoview usage is similar to Mediaplayer.</p></p><p><p></p></p><p><p>Android Learning audio, Video</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.