• Android&简单Mp3播放


    activity_main.xml
     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context=".MainActivity"
    10     android:orientation="vertical" >
    11 
    12     <TextView
    13         android:layout_width="wrap_content"
    14         android:layout_height="wrap_content"
    15         android:text="@string/hello_world" />
    16     
    17     <EditText
    18         android:id="@+id/myPathEditText"
    19         android:layout_width="fill_parent"
    20         android:layout_height="wrap_content"
    21         android:hint="Please enter the path."/>
    22     <LinearLayout    android:layout_width="match_parent"
    23         android:layout_height="match_parent"
    24         android:orientation="horizontal">
    25         <Button
    26             android:id="@+id/startButton"
    27             android:text="Start"
    28             android:layout_width="wrap_content"
    29             android:layout_height="wrap_content"
    30             />
    31         <Button
    32             android:id="@+id/stopButton"
    33             android:text="Stop"
    34             android:layout_width="wrap_content"
    35             android:layout_height="wrap_content"
    36             />
    37     </LinearLayout>
    38 </LinearLayout>
    MainActivity.java
     1 package com.zachary.mymp3;
     2 
     3 import android.app.Activity;
     4 import android.content.Intent;
     5 import android.os.Bundle;
     6 import android.view.Menu;
     7 import android.view.View;
     8 import android.view.View.OnClickListener;
     9 import android.widget.Button;
    10 import android.widget.EditText;
    11 import android.widget.Toast;
    12 
    13 public class MainActivity extends Activity {
    14     private Button startButton = null;
    15     private Button stopButton = null;
    16     private EditText myPathEditText = null;
    17     private boolean isPlaying = false;
    18     private String path = null;
    19     @Override
    20     protected void onCreate(Bundle savedInstanceState) {
    21         super.onCreate(savedInstanceState);
    22         setContentView(R.layout.activity_main);
    23         startButton = (Button)findViewById(R.id.startButton);
    24         stopButton = (Button)findViewById(R.id.stopButton);
    25         myPathEditText = (EditText)findViewById(R.id.myPathEditText);
    26         
    27         startButton.setOnClickListener(new OnClickListener(){
    28 
    29             @Override
    30             public void onClick(View arg0) {
    31                 // TODO Auto-generated method stub
    32                 path = myPathEditText.getText().toString();
    33                 System.out.println("Path---->"+path);
    34                 if(!isPlaying) {
    35                     if(path!=null){
    36                         isPlaying = true;
    37                         Intent    intent = new Intent(MainActivity.this, MediaPlayerService.class);
    38                         intent.putExtra("path", path);
    39                         MainActivity.this.startService(intent);
    40                         //Toast.makeText(MainActivity.this, "MediaPlayer is Begining!", Toast.LENGTH_LONG).show();
    41                     }
    42                 }
    43             }
    44             
    45         });
    46         
    47         stopButton.setOnClickListener(new OnClickListener(){
    48 
    49             @Override
    50             public void onClick(View v) {
    51                 // TODO Auto-generated method stub
    52                 if(isPlaying){
    53                 MainActivity.this.stopService(new Intent(MainActivity.this, MediaPlayerService.class));
    54                 isPlaying = false;
    55                 //Toast.makeText(MainActivity.this, "MediaPlayer is Stop!", Toast.LENGTH_LONG).show();
    56                 }
    57             }
    58             
    59         });
    60         
    61     }
    62 
    63     @Override
    64     public boolean onCreateOptionsMenu(Menu menu) {
    65         // Inflate the menu; this adds items to the action bar if it is present.
    66         getMenuInflater().inflate(R.menu.main, menu);
    67         return true;
    68     }
    69 
    70 }
    MediaPlayerServie.java
     1 package com.zachary.mymp3;
     2 
     3 import android.app.Service;
     4 import android.content.Intent;
     5 import android.media.MediaPlayer;
     6 import android.net.Uri;
     7 import android.os.IBinder;
     8 import android.util.Log;
     9 import android.widget.Toast;
    10 
    11 public class MediaPlayerService extends Service {
    12 
    13     private static final String TAG = "Media Player Service";
    14     private MediaPlayer player = null;
    15     @Override
    16     public IBinder onBind(Intent arg0) {
    17         // TODO Auto-generated method stub
    18         return null;
    19     }
    20     
    21     @Override
    22     public void onCreate() {
    23         Toast.makeText(this, "MediaPlayerService Created", Toast.LENGTH_LONG).show();
    24         Log.i(TAG, "onCreate");
    25         System.out.println("onCreate");
    26     }
    27     
    28     @Override
    29     public void onStart(Intent intent, int startId) {
    30         Toast.makeText(this, "MediaPlayerService Start", Toast.LENGTH_LONG).show();
    31         
    32         String path = intent.getStringExtra("path");
    33         //String path="http://douban.fm/?start=1466389g8675g1000388&cid=1000388";
    34     
    35             Log.i(TAG, "onStart");
    36             System.out.println("onStart");
    37             Uri playerUri = Uri.parse(path);
    38             if (player != null) { 
    39                 player.stop(); 
    40             } 
    41             player = MediaPlayer.create(MediaPlayerService.this, playerUri);
    42             player.start();
    43     }
    44     
    45     @Override
    46     public void onDestroy() {
    47         super.onDestroy();
    48         Toast.makeText(this, "MediaPlayerService Stoped", Toast.LENGTH_LONG).show();
    49         Log.i(TAG, "onDestory");
    50         System.out.println("onDestory");
    51         if(player != null) {
    52         player.release();
    53         player = null;
    54         }
    55     }
    56 
    57 }
  • 相关阅读:
    Windows打开软件老是弹出无法验证发布者
    SpringMvc接受特殊符号参数被转义
    时代更替中的方正
    你应该知道的c# 反射详解
    C#使用System.Data.SQLite操作SQLite
    C# 动态调用WebService
    C# API: 生成和读取Excel文件
    11个强大的Visual Studio调试小技巧
    .Net 垃圾回收和大对象处理
    Visual Studio原生开发的10个调试技巧(一)
  • 原文地址:https://www.cnblogs.com/wizzhangquan/p/2979382.html
Copyright © 2020-2023  润新知