ExternalStorage存放音乐,通过Notification播放音乐,并震动的Demo
来源:互联网 发布:c语言控制鼠标点击 编辑:程序博客网 时间:2024/05/01 17:16
首先是AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ty.externstore_demo1"> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"> </uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"> </uses-permission> <uses-permission android:name="android.permission.VIBRATE"> </uses-permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ImageViewClass"> </activity> </application></manifest>
package com.ty.externstore_demo1;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.net.Uri;import android.os.Environment;import android.support.annotation.NonNull;import android.support.v4.app.NotificationCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.Toast;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class MainActivity extends AppCompatActivity { String saved_FilePath; Button btn_Play; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn_Save = (Button) findViewById(R.id.BTN_Save); btn_Play = (Button) findViewById(R.id.BTN_Play); CheckBtnEnable(btn_Save); } public void SendNotification_Btn(View v) { PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, new Intent(this,ImageViewClass.class), PendingIntent.FLAG_UPDATE_CURRENT); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setContentTitle("播放音乐") .setTicker("通知来啦") .setSmallIcon(R.mipmap.ic_launcher) .setContentText(saved_FilePath) .setAutoCancel(true) .setNumber(1) .setSound(Uri.parse(saved_FilePath)) .setPriority(NotificationCompat.PRIORITY_MAX) .setContentIntent(pendingIntent); notificationManager.notify(1, builder.build()); } public void Show_Btn(View v) { File dir = Environment.getExternalStorageDirectory(); String[] list = dir.list(); for(String str:list) { Log.i("ListInformation", str); } } public void Save_Btn(View v) { saved_FilePath = SaveToExtendStorage(R.raw.shake_sound_male, "myMusic.mp3"); } private void CheckBtnEnable(Button btn) { String stats = Environment.getExternalStorageState(); if(!stats.equals(Environment.MEDIA_MOUNTED)) { btn.setEnabled(false); Toast.makeText(this,"SD Card Error, Cannot Save Music", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this,"You Can Save Music", Toast.LENGTH_LONG).show(); } } @NonNull private String SaveToExtendStorage(int resourceID, String name) { BufferedInputStream bis = new BufferedInputStream(getResources().openRawResource(resourceID)); int len; byte[] b = new byte[1024]; File savedPath = new File(Environment.getExternalStorageDirectory(), name); try { OutputStream os = new FileOutputStream(savedPath); while((len = bis.read(b)) != -1) { os.write(b,0,len);//第二个参数,代表从源b的第几个偏移量开始读 os.flush(); } os.close(); bis.close(); btn_Play.setEnabled(true); Toast.makeText(this,savedPath.getPath()+"Saving Finished", Toast.LENGTH_LONG).show(); } catch (IOException e) { e.printStackTrace(); } return savedPath.getPath(); }}
ImageViewClass就是通过PendingIntent启动的Activity
package com.ty.externstore_demo1;import android.os.Bundle;import android.os.Vibrator;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;/** * Created by IT on 2017/2/9. */public class ImageViewClass extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_imageview); Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); vibrator.vibrate(new long[]{500,2000,500,3000}, -1); }}
Main的布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.ty.externstore_demo1.MainActivity"> <Button android:id="@+id/BTN_Save" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Save Music" android:onClick="Save_Btn"/> <Button android:id="@+id/BTN_Show" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Show Dir" android:onClick="Show_Btn"/> <Button android:id="@+id/BTN_Play" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Play Music" android:onClick="SendNotification_Btn" android:enabled="false"/></LinearLayout>
ImageViewClass的布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/b"/></LinearLayout>
0 0
- ExternalStorage存放音乐,通过Notification播放音乐,并震动的Demo
- Unity 摇一摇震动并随机播放音乐
- android音乐播放demo
- android_56_Service_音乐播放demo
- 点击按钮播放音乐的demo
- 能实现播放本地音乐的Demo
- ios边播放音乐边震动
- 短音频\音乐播放+震动(和手机来电一样震动音乐停震动停)
- Android音乐播放器---实现Notification控制音乐播放
- Android音乐播放器---实现Notification控制音乐播放
- Android音乐播放器---实现Notification控制音乐播放
- Android音乐播放器---实现Notification控制音乐播放
- 本地音乐查询播放Demo
- 音乐播放demo知识点总结
- Android音乐播放器demo
- notification+service+broadcastreceiver实现简单的音乐播放器
- 音乐播放器(2)--歌曲数据存放
- android手机震动、提示音、播放系统音乐和研究7.0,6.0弹出Notification出现crash问题
- PHP检测文件方法,类方法是否存在
- directx初第七卷
- 算法提高 盾神与条状项链
- directx初第八卷
- 后面抽空看一下
- ExternalStorage存放音乐,通过Notification播放音乐,并震动的Demo
- apue:进程关系
- 泛型的参数的传递示意图
- 深入分析js中的constructor 和prototype
- 【NOI2012】随机数生成器
- CJOJ P2317 JesseLiu的预算方案
- 为什么1000 == 1000返回为False,而100 == 100会返回为True?
- 【JZOJ 3693】 慎二的随机数列
- 用51单片机+11.0592的晶振,如何产生115200的波特率