Use Mediaplay to write a player, mediaplay player

Source: Internet
Author: User
Tags call back

Use Mediaplay to write a player, mediaplay player

We know that android has limited video playback capabilities .. Let's start with a Demo.

An immature player, http://www.eoemarket.com/soft/370334.html, is being completed.

Package cn. marsXTU. mediaplayer; import android. media. audioManager; import android. media. mediaPlayer; import android. media. mediaPlayer. onCompletionListener; import android. OS. bundle; import android. OS. systemClock; import android. view. surface; import android. view. surfaceHolder; import android. view. surfaceView; import android. view. view; import android. widget. editText; import android. widget. seekBar; import android. Widget. toast; import android. widget. seekBar. onSeekBarChangeListener; import android. widget. textView; import android. app. activity;/*** 1. ensure that the function is enhanced when the music can be played * @ author hjl */public class MainActivity extends Activity implements OnSeekBarChangeListener, OnCompletionListener {private boolean isStopUpdatingProgress = false; private EditText etPath; private MediaPlayer mMediapPlayer; private SeekBar mSeekba R; private TextView tvCurrentTime; private TextView tvTotalTime; private final int NORMAL = 0; // idle private final int PLAYING = 1; // private final int PAUSING = 2 in playback; // pause private final int STOPING = 3; // stop private int currentstate = NORMAL; // the current state of the player, the default value is idle. // use actions to dispel worries. private SurfaceHolder holder; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. a Ctivity_main); etPath = (EditText) findViewById (R. id. et_path); mSeekbar = (SeekBar) findViewById (R. id. sb_progress); tvCurrentTime = (TextView) findViewById (R. id. TV _current_time); tvTotalTime = (TextView) findViewById (R. id. TV _total_time); mSeekbar. setOnSeekBarChangeListener (this); SurfaceView mSurfaceView = (SurfaceView) findViewById (R. id. surfaceview); holder = mSurfaceView. getHolder (); // SurfaceView help Class Object // uses its own internal double buffer, while Is waiting for others to push data holder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS);}/*** start * @ param v */public void start (View v) {if (mMediapPlayer! = Null) {if (currentstate! = PAUSING) {mMediapPlayer. start (); currentstate = PLAYING; isStopUpdatingProgress = false; // set it to falsereturn every time the refresh thread is called; // The following judgment perfectly solves the problem of re-playing after stopping and releasing two resources} else if (currentstate = STOPING) {mMediapPlayer. reset (); mMediapPlayer. release () ;}} play () ;}/ *** stop * @ param v */public void stop (View v) {if (mMediapPlayer! = Null) {mMediapPlayer. stop () ;}/ *** file in the playback input box */private void play () {String path = etPath. getText (). toString (). trim (); mMediapPlayer = new MediaPlayer (); try {// set the Data Type mMediapPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC); // set the position of the following PLAYER: mMediapPlayer. setDisplay (holder); mMediapPlayer. setDataSource (path); mMediapPlayer. prepare (); mMediapPlayer. start (); mMediapPlayer. setOnCompletionListener (this); // display the current player Set to: currentstate = PLAYING in the video; // obtain the total length of the music file and set it to seekbar as the maximum value int duration = mMediapPlayer. getDuration (); // The total duration of mSeekbar. setMax (duration); // display the total time on textView int m = duration/1000/60; int s = duration/1000% 60; tvTotalTime. setText ("/" + m + ":" + s); tvCurrentTime. setText ("00:00"); isStopUpdatingProgress = false; new Thread (new UpdateProgressRunnable ()). start ();} catch (Exception e) {e. printStackTrace () ;}}/*** pause * @ param v */ Public void pause (View v) {if (mMediapPlayer! = Null & effectstate = PLAYING) {mMediapPlayer. pause (); currentstate = PAUSING; isStopUpdatingProgress = true; // stop refreshing the master thread}/*** replay * @ param v */public void restart (View v) {if (mMediapPlayer! = Null) {mMediapPlayer. reset (); mMediapPlayer. release (); play () ;}@overridepublic void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {}@ Overridepublic void onStartTrackingTouch (SeekBar seekBar) {progress = true; // when you start dragging, stop the refresh thread.} @ Overridepublic void onStopTrackingTouch (SeekBar seekBar) {int progress = seekBar. getProgress (); // The player switches to the specified progress position. seekTo (progress ); IsStopUpdatingProgress = false; new Thread (new UpdateProgressRunnable ()). start ();}/*** call back this method when playback is complete */@ Overridepublic void onCompletion (MediaPlayer mp) {Toast. makeText (this, "playing is finished, playing again", 0 ). show (); mp. start ();}/*** refresh progress and time task * @ author hjl **/class UpdateProgressRunnable implements Runnable {@ Overridepublic void run () {// take the progress of the current playing every second and set it to seekbarwhile (! IsStopUpdatingProgress) {// get the current progress int currentPosition = mMediapPlayer. getCurrentPosition (); mSeekbar. setProgress (currentPosition); final int m = currentPosition/1000/60; final int s = currentPosition/1000% 60; // The runable object specified by this method will execute the main thread (in the UI thread) runOnUiThread (new Runnable () {@ Overridepublic void run () {tvCurrentTime. setText (m + ":" + s) ;}}); SystemClock. sleep (1000 );}}}}




How can this problem be solved when Windows Media Play is set as the default player on the computer?

Haha, Windows Media Player may not be able to play all audio and video files. If you have to change it, select a file you want to open by default, such *. right-click the wma file and select "open mode" and "Select Program". There will be "Recommended programs" and "other programs". Find your Media Player and select it, then, click "open this file with the selected program" and click "OK! So... in the future, all wma will be opened using Media Player ...... hey hey... we recommend that you use media player classic (MPC). Now I am using QQ audio and video, which is more convenient :)
 
A c # MP3 player created by referencing Mediaplayer

Create a new form, open the toolbar on the visual interface, right click the toolbar -- select item -- com component --- window mediaplayer -- OK. Drag the mediaplayer control in the toolbar to the blank form area, drag an openfiledialog control to the form interface, and drag a buttom control to the mediaplayer. Double-click the buttom control and copy the following code.

String resultFile = "";
OpenFileDialog openFileDialog1 = new OpenFileDialog ();
OpenFileDialog1.InitialDirectory = "D :\\";
OpenFileDialog1.Filter = "All files (*. *) | *. * | wmv files (*. wmv) | *. wmv ";
OpenFileDialog1.FilterIndex = 2;
OpenFileDialog1.RestoreDirectory = true;
If (openFileDialog1.ShowDialog () = DialogResult. OK)
ResultFile = openFileDialog1.FileName;
AxWindowsMediaPlayer1.URL = resultFile;

Related Article

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.