Android development path-Implementation of music player

Source: Internet
Author: User

Strings. xml

<? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "app_name"> MusicPlayer </string> <string name = "music_name"> tracks </string> <string name = "play_text"> play </ string> <string name = "pause_text"> pause </string> <string name = "continue_text"> continue </string> <string name = "reset_text"> Reset </string> <string name = "stop_text"> stop </string> <string name = "choose_text"> select </string> <string name = "notfoundfile_text"> the media file does not exist </ string> <string name = "notfoundSdcard_text"> SDcard does not exist </string> </resources>

 

 

 

Main. xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/music_name" />    <TableLayout         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:stretchColumns="0" >        <TableRow >        <EditText            android:id="@+id/musicEt"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="l.mp3" />        <Button            android:id="@+id/chooseBtn"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/choose_text" />                </TableRow>    </TableLayout>    <SeekBar        android:id="@+id/seekBar"        android:layout_width="fill_parent"        android:layout_height="wrap_content" />    <SeekBar        android:id="@+id/seekBarSound"       android:layout_width="fill_parent"        android:layout_height="wrap_content"              android:max="100"               android:progress="10"/>              />        <TextView        android:id="@+id/time"         android:layout_width="fill_parent"        android:layout_height="wrap_content"        />    <TableLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:stretchColumns="*" >        <TableRow >            <Button                android:id="@+id/playBtn"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/play_text" />            <Button                android:id="@+id/pauseBtn"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/pause_text" />            <Button                android:id="@+id/stopBtn"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/stop_text" />        </TableRow>    </TableLayout></LinearLayout>

 

MusicPlayerActivity. java

Package cn. csdn. playle; import java. io. file; import java. io. IOException; import android. app. activity; import android. content. context; import android. content. intent; import android. media. audioManager; import android. media. mediaPlayer; import android. OS. bundle; import android. OS. environment; import android. OS. handler; import android. util. log; import android. view. view; import android. view. view. onClickListener; impo Rt android. widget. button; import android. widget. editText; import android. widget. seekBar; import android. widget. seekBar. onSeekBarChangeListener; import android. widget. textView; import android. widget. toast; public class MusicPlayerActivity extends Activity implements OnClickListener, OnSeekBarChangeListener {EditText musicEt; Button playBtn, pauseBtn, stopBtn, chooseBtn; TextView time; AudioManager audiomanag E; int maxVolume, currentVolume; SeekBar seekBar, seekBarSound; MediaPlayer player = null; File file = null; int position = 0; int I = 0; Handler handler = new Handler (); public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); player = new MediaPlayer (); findViews ();} private void findViews () {musicEt = (EditText) this. findViewById (R. id. musicEt); play Btn = (Button) this. findViewById (R. id. playBtn); pauseBtn = (Button) this. findViewById (R. id. pauseBtn); stopBtn = (Button) this. findViewById (R. id. stopBtn); chooseBtn = (Button) this. findViewById (R. id. chooseBtn); seekBar = (SeekBar) this. findViewById (R. id. seekBar); seekBarSound = (SeekBar) this. findViewById (R. id. seekBarSound); time = (TextView) this. findViewById (R. id. time); time. setOnClickListener (this); pla YBtn. setOnClickListener (this); pauseBtn. setOnClickListener (this); stopBtn. setOnClickListener (this); stopBtn. setEnabled (false); pauseBtn. setEnabled (false); seekBar. setOnSeekBarChangeListener (this); chooseBtn. setOnClickListener (new OnClickListener () {public void onClick (View v) {Intent intent = new Intent (MusicPlayerActivity. this, MusicListActivity. class); startActivity (intent) ;}}); audiomanage = (AudioManag Er) getSystemService (Context. AUDIO_SERVICE); maxVolume = audiomanage. getStreamMaxVolume (AudioManager. STREAM_MUSIC); // obtain the maximum system volume seekBarSound. setMax (maxVolume); // match currentVolume = audiomanage with the maximum sound of the system. getStreamVolume (AudioManager. STREAM_MUSIC); // obtain the current value seekBarSound. setProgress (currentVolume); seekBarSound. setOnSeekBarChangeListener (new OnSeekBarChangeListener () // listener {public void onProgressChan Ged (SeekBar arg0, int progress, boolean fromUser) {audiomanage. setStreamVolume (AudioManager. STREAM_MUSIC, progress, 0); currentVolume = audiomanage. getStreamVolume (AudioManager. STREAM_MUSIC); // obtain the current value seekBarSound. setProgress (currentVolume);} public void onStartTrackingTouch (SeekBar seekBar) {} public void onStopTrackingTouch (SeekBar seekBar) {}}); Intent intent = this. getIntent (); String name = intent. ge TStringExtra ("name"); musicEt. setText (name);} public void onClick (View v) {String fileName = musicEt. getText (). toString (). trim (); // obtain whether the status of the sdcard is loaded (MEDIA_MOUNTED loading status) if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {file = new File (Environment. getExternalStorageDirectory (), fileName); // checks whether the played media file exists if (file. exists () {try {switch (v. getId () {// returns an int value case R. id. pla YBtn: playMusic (file); break; case R. id. pauseBtn: if (player. isPlaying () {player. pause (); pauseBtn. setText (R. string. continue_text);} else {player. start (); pauseBtn. setText (R. string. pause_text);} break; case R. id. stopBtn: player. stop (); stopBtn. setEnabled (false); seekBar. setProgress (0); if (playBtn. getText (). toString (). equals ("reset") {playBtn. setText (R. string. play_text);} if (pauseBtn. getText (). toString (). equal S ("continue") {pauseBtn. setText (R. string. pause_text); pauseBtn. setEnabled (false);} else {pauseBtn. setEnabled (false);} break;} catch (IllegalArgumentException e) {Log. e ("TAG", e. toString ();} catch (IllegalStateException e) {Log. e ("TAG", e. toString ();} catch (IOException e) {Log. e ("TAG", e. toString () ;}} else {Toast. makeText (this, R. string. notfoundfile_text, Toast. LENGTH_LONG ). show () ;}} else {Toast. makeTe Xt (this, R. string. notfoundSdcard_text, Toast. LENGTH_LONG). show () ;}} protected void onDestroy () {if (player! = Null) {if (player. isPlaying () {player. stop ();} player. release ();} super. onDestroy ();} protected void onPause () {if (player! = Null) {if (player. isPlaying () {player. pause () ;}} super. onPause ();} private void playMusic (File file) throws IllegalStateException, IOException {if (musicEt. getText (). toString () = null | "". equals (musicEt. getText (). toString () {Toast. makeText (this, "No selected song", Toast. LENGTH_LONG ). show ();} else {if (player! = Null) {pauseBtn. setEnabled (true); player. reset (); player. setDataSource (file. getAbsolutePath (); player. prepare (); player. start (); playBtn. setText (R. string. reset_text); run ();} if (playBtn. getText (). toString (). equals ("reset") {pauseBtn. setEnabled (true); stopBtn. setEnabled (true);} if (pauseBtn. getText (). toString (). equals ("continue") {pauseBtn. setText (R. string. pause_text) ;}}} private void run () {new Thread (new Runna Ble () {public void run () {while (player! = Null) {int TIME = player. getDuration (); seekBar. setMax (TIME); // obtain the maximum position of a song = player. getCurrentPosition (); // returns the current playback progress value seekBar. setProgress (position); try {Thread. sleep (3000);} catch (InterruptedException e) {e. printStackTrace ();}}}}). start ();} public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {} public void onStartTrackingTouch (SeekBar seekBar) {player. seekTo (seekBar. getProgress ();} public void onStopTrackingTouch (SeekBar seekBar) {player. seekTo (seekBar. getProgress ());}}

 

 

 

:

 

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.