zoukankan      html  css  js  c++  java
  • 安卓学习03

    title: 安卓学习03
    date: 2020-02-03 20:28:30
    tags:

    今日学习了mediaplayer的简单封装使用以及引导页。

    <!--more -->

    1、mediaplayer的封装

    package com.example.framework.manager;
    ​
    import android.content.res.AssetFileDescriptor;
    import android.media.MediaPlayer;
    import android.os.Build;
    import android.os.Handler;
    import android.os.Message;
    ​
    import androidx.annotation.NonNull;
    import androidx.annotation.RequiresApi;
    ​
    import com.example.framework.utils.LogUtils;
    ​
    import java.io.IOException;
    ​
    public class MediaPlayerManager {
    ​
        //播放
        public static final int MEDIA_STATUS_PLAY = 0;
        //暂停
        public static final int MEDIA_STATUS_PAUSE = 1;
        //停止
        public static final int MEDIA_STATUS_STOP = 2;
        //当前状态
        public static int MEDIA_STATUS = MEDIA_STATUS_STOP;
    ​
        public static final int H_PROGRESS = 1000;
    ​
        private MediaPlayer mediaPlayer;
        private OnMusicProgressListener musicProgressListener;
        private Handler handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage( Message msg) {
                switch (msg.what){
                    case H_PROGRESS:
                        if (musicProgressListener != null){
                            int currentPosition = getCurrentPosition();
                            int p = (int) (((float)currentPosition)/((float)getDuration()) * 100);
                            musicProgressListener.OnProgress(currentPosition,p);
                            handler.sendEmptyMessageDelayed(H_PROGRESS,1000);
                        }
                        break;
                }
                return false;
            }
        });
    ​
        public MediaPlayerManager() {
            mediaPlayer = new MediaPlayer();
        }
    ​
        @RequiresApi(api = Build.VERSION_CODES.N)
        public void startPlay(AssetFileDescriptor path){
            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(path);
                mediaPlayer.prepare();
                mediaPlayer.start();
                MEDIA_STATUS = MEDIA_STATUS_PLAY;
                handler.sendEmptyMessage(H_PROGRESS);
            } catch (IOException e) {
                LogUtils.e(e.toString());
                e.printStackTrace();
            }
        }
    ​
        //暂停播放
        public void pausePlay(){
            if (mediaPlayer.isPlaying()){
                mediaPlayer.pause();
                MEDIA_STATUS = MEDIA_STATUS_PAUSE;
                handler.removeMessages(H_PROGRESS);
            }
        }
    ​
        //继续播放
        public void continuePlay(){
            mediaPlayer.start();
            MEDIA_STATUS = MEDIA_STATUS_PLAY;
            handler.sendEmptyMessage(H_PROGRESS);
        }
    ​
        //停止播放
        public void stopPlay(){
            mediaPlayer.stop();
            MEDIA_STATUS = MEDIA_STATUS_STOP;
            handler.removeMessages(H_PROGRESS);
        }
    ​
        //获取当前位置
        public int getCurrentPosition(){
            return mediaPlayer.getCurrentPosition();
        }
    ​
        //获取音乐的总时间
        public int getDuration(){
            return mediaPlayer.getDuration();
        }
    ​
        //是否循环
        public void setLooping(boolean isLooping){
            mediaPlayer.setLooping(isLooping);
        }
    ​
        //跳转位置
        public void seekTo(int mesc){
            mediaPlayer.seekTo(mesc);
        }
    ​
        //播放结束
        public void setOnCompletionListener(MediaPlayer.OnCompletionListener listener){
            mediaPlayer.setOnCompletionListener(listener);
        }
    ​
        //播放错误
        public void setOnErrorListener(MediaPlayer.OnErrorListener listener){
            mediaPlayer.setOnErrorListener(listener);
        }
    ​
        //播放进度
        public void setOnProgressListener(OnMusicProgressListener listener){
            musicProgressListener = listener;
        }
    ​
        public interface OnMusicProgressListener{
            void OnProgress(int progress,int pos);
        }
    }

    在页面的使用代码为:

    public class MainActivity extends BaseUIActivity {
    ​
        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            final MediaPlayerManager mediaPlayerManager = new MediaPlayerManager();
            AssetFileDescriptor fileDescriptor = getResources().openRawResourceFd(R.raw.guide);
            mediaPlayerManager.startPlay(fileDescriptor);
            mediaPlayerManager.setOnProgressListener(new MediaPlayerManager.OnMusicProgressListener() {
                @Override
                public void OnProgress(int progress,int pos) {
    ​
                }
            });
        }
    }

    这样就可以简单的找到项目中的一个音乐并播放了。

    2、刘海屏和水滴屏的适配

    在AndroidManifest.xml中添加以下适配的代码:

    <!--        刘海屏适配-->
            <meta-data android:name="android.max_aspect"
                android:value="2.2" /><!--        小米水滴屏适配-->
            <meta-data
                android:name="notch.config"
                android:value="portrait|landscape"/>

    3、引导页

    package com.example.meet.UI;
    ​
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.text.TextUtils;
    ​
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    ​
    import com.example.framework.entity.Constants;
    import com.example.framework.utils.SpUtils;
    import com.example.meet.MainActivity;
    import com.example.meet.R;
    ​
    public class IndexActivity extends AppCompatActivity {
    ​
        /*
        1、把启动页全屏
        2、延迟进入主页
        3、根据具体逻辑是进入主页还是引导页还是登录页
        4、适配刘海屏
         */
        private static final int SKIN_MAIN = 1000;
        private Handler handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage( Message msg) {
                switch (msg.what){
                    case SKIN_MAIN:
                        startMain();
                        break;
                }
                return false;
            }
        });
    ​
        @Override
        protected void onCreate( Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_index);
            handler.sendEmptyMessageDelayed(SKIN_MAIN,2000);
        }
    ​
        /**
         * 进入主页
         */
        private void startMain(){
            //判断APP是否是第一次打开
            SpUtils.getInstance().initSp(this);
            boolean isFirstApp = SpUtils.getInstance().getBoolean(Constants.SP_IS_FIRST_APP, true);
            Intent intent = new Intent();
            if (isFirstApp){
                //跳转到引导页
                intent.setClass(this,GuideActivity.class);
                //设置为非第一次启动
                SpUtils.getInstance().putBoolean(Constants.SP_IS_FIRST_APP, false);
            }else {
                //判断是否登陆过
                String token = SpUtils.getInstance().getString(Constants.SP_TOKEN, "");
                if (TextUtils.isEmpty(token)){
                    //跳转到登录页
                    intent.setClass(this,LoginActivity.class);
                }else {
                    //跳转到主页面
                    intent.setClass(this, MainActivity.class);
                }
            }
    ​
            startActivity(intent);
            finish();
        }
    }

     

  • 相关阅读:
    HDU 跑跑卡丁车
    螺旋模型
    原型模型
    CSS匹配规则参考
    索引调优
    动态加载外部css或js文件
    des算法的C#实现
    @@RowCount和“SET NOCOUNT ON”在触发器中使用的先后顺序引起的问题
    WebService生成XML文档时出错。不应是类型XXXX。使用XmlInclude或SoapInclude属性静态指定非已知的类型。
    Sql获取星期几的方法
  • 原文地址:https://www.cnblogs.com/wuren-best/p/12257234.html
Copyright © 2011-2022 走看看