java攻城狮之路(Android篇)--MP3、MP4、拍照、国际化、样式主题、图片移动和缩放

一、MP3播放器

查看Android API文档可以看到MediaPlayer状态转换图:

技术分享

练习:

技术分享

技术分享
package com.shellway.mp3player;

import java.io.File;
import java.io.IOException;

import android.support.v7.app.ActionBarActivity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
    //媒体播放器
    private MediaPlayer mp;
    private EditText songName;
    //之前是否暂停过
    private boolean pause;
    private File file;
    private TelephonyManager tm;
    //暂停保存点
    private int position = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        songName = (EditText) findViewById(R.id.song_name);
        
        tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        tm.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
    }
    
    private class MyPhoneStateListener extends PhoneStateListener{
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            // TODO Auto-generated method stub
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE://闲置状态
                if (position > 0 && mp != null) {
                    //跳转到指定的保存点位置
                    mp.seekTo(position);
                    mp.start();
                }
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK://接听状态
                if(mp != null){
                    if(mp.isPlaying()){
                        position = mp.getCurrentPosition();
                        mp.pause();
                    }
                }
                break;
            case TelephonyManager.CALL_STATE_RINGING://响铃状态
                
                 if (mp.isPlaying()) {
                     position = mp.getCurrentPosition();
                     mp.pause();
                    }
                break;
                
            default:
                break;
            }
        }
    }
//这里是利用Activity生命周期的方法监听来电时暂停播放
//  @Override
//  protected void onResume() {
//      // TODO Auto-generated method stub
//      super.onResume();
//      if(position > 0){
//          //跳转到指定的位置
//          mp.seekTo(position);
//          mp.start();
//      }
//  }
//  
//  @Override
//  protected void onPause() {
//      // TODO Auto-generated method stub
//      super.onPause();
//      position = mp.getCurrentPosition();
//      mp.pause();
//  }
    
    public void play(View view){
        String name = songName.getText().toString();
        
        file = new File(Environment.getExternalStorageDirectory(),name+".mp3");
        
            if (!file.exists()) {
                Toast.makeText(this, "文件不存在", Toast.LENGTH_SHORT).show();
                mp=null;
            }else{
                if (mp!=null) {
                    if (mp.isPlaying()) {
                        Toast.makeText(this, "歌曲已经在播放中", Toast.LENGTH_SHORT).show();
                    }else{
                        play();
                    }
                }else{
                    play();
                }
         }
    }
    
    public void play(){
        try {
            mp = new MediaPlayer();
            //置为初始状态
            mp.reset();
            //指定要播放的文件
            mp.setDataSource(file.getAbsolutePath());
            //准备(缓冲)
            mp.prepare();
            //设置缓冲完成监听
            mp.setOnPreparedListener(new MyOnPreparedListener());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    private class MyOnPreparedListener implements OnPreparedListener{

        @Override
        public void onPrepared(MediaPlayer mp) {
            // TODO Auto-generated method stub
            //播放
            mp.start();
        }
    }
    
    public void pause(View view){
        if (mp.isPlaying()) {
            //暂停播放
            mp.pause();
            Button bt = (Button) view;
            bt.setText("继续");
            pause = true;
        }else{
            if (pause) {
                mp.start();
                Button bt = (Button) view;
                bt.setText("停止");
            }
        }
    }
    
    public void stop(View view){
        if (mp!=null) {
            //停止播放
            mp.stop();
        }
    }
    
    public void reset(View view){
        if (mp!=null) {
            mp.start();
        }
    }
    
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (mp != null) {
            mp.release();
            mp = null;
        }
    }
}
MainActivity.java
技术分享
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="请输入歌曲名" />
    <EditText 
        android:id="@+id/song_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Complicated"
        />
  <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
    <Button 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="play"
        android:text="播放"
        />
    <Button 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="pause"
        android:text="暂停"
        />
    <Button 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="stop"
        android:text="停止"
        />
    <Button 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="reset"
        android:text="重播"
        />
  </LinearLayout>
</LinearLayout>
activity_main.xml
技术分享
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.shellway.mp3player"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <!-- 监听来电状态需要读取电话状态权限 -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
AndroidManifest.xml

运行结果截图:

技术分享

二:MP4播放器

 

 

 

 

 

稍等,努力更新中。。。。

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。